package org.example.config.interceptor;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.config.UserContextHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.HashMap;
import java.util.Map;

public class UserInteceptor implements HandlerInterceptor {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if (token == null) return false;
        String username = stringRedisTemplate.opsForValue().get(token);
        if (username == null) {
            return false;
        }
        Map<String, String> map = new HashMap<>();
        map.put("token", "98e7833e-aabc-4989-b389-49843e210fea");
        map.put("username", username);
        UserContextHolder.set(map);
        return true;
    }
}
